﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.Web;
using DHQGHN.DAL.BO.DanhMuc;
using DHQGHN.DAL.Models.DanhMuc;

namespace DHQGHN.Services.DanhMuc
{
    public class AdminUnitService : IAdminUnitService
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(AdminUnitExtraService));

        private AdministrativeUnitBO adminUnitBO;
        #region Constructors
        public AdminUnitService()
        {
            //string connString = ConfigurationManager.ConnectionStrings["DHQGHN_TDT.ConnectionString"].ConnectionString;
            adminUnitBO = new AdministrativeUnitBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
        }
        #endregion


        public DAL.Models.DanhMuc.AdministrativeUnit GetByID(string adminUnitExtraID)
        {
            try
            {
                string id = "-1";

                if (string.IsNullOrEmpty(adminUnitExtraID))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                //if (long.TryParse(adminUnitExtraID, out id) == false)
                //{
                //    Fault.ValidationFault fault = new Fault.ValidationFault()
                //    {
                //        Result = false,
                //        Message = "Tham số areaID sai định dạng!",
                //        Description = "Tham số areaID sai định dạng!"
                //    };

                //    throw new FaultException<Fault.ValidationFault>(fault);
                //}
                id = adminUnitExtraID;
                return adminUnitBO.GetById(id);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="lastUpdated"></param>
        /// <returns></returns>
        public AdministrativeUnitTablet GetNewListsTablet(string lastUpdated)
        {
            //HeaderInfo h = new HeaderInfo();
            AdministrativeUnitTablet jret = new AdministrativeUnitTablet();
            try
            {
                if (string.IsNullOrEmpty(lastUpdated))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };


                    jret.ErrorCode = -1;
                    jret.ErrorMessage = "Thiếu tham số";
                    jret.CurrentTime = DHQGHN.Common.Utils.GetCurrentTimeNow();
                    jret.Data = null;

                    return jret;
                }

                string formatString = "yyyyMMddHHmmss";
                //string sample = "20100611221912";
                DateTime _lastUpdated;
                try
                {
                    _lastUpdated = DateTime.ParseExact(lastUpdated, formatString, null);
                }
                catch
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Tham số PageSize (hoặc PageIdx) sai định dạng!",
                        Description = "Tham số PageSize (hoặc PageIdx) sai định dạng!"
                    };

                    jret.ErrorCode = -2;
                    jret.ErrorMessage = "Tham số PageSize (hoặc PageIdx) sai định dạng!";
                    jret.CurrentTime = DHQGHN.Common.Utils.GetCurrentTimeNow();
                    jret.Data = null;

                    return jret;


                    //throw new FaultException<Fault.ValidationFault>(fault);

                }

                List<AdministrativeUnit> ret = adminUnitBO.GetNewLists(_lastUpdated);

                jret.ErrorCode = 200;
                jret.ErrorMessage = "OK";
                jret.CurrentTime = DHQGHN.Common.Utils.GetCurrentTimeNow();
                jret.Data = ret;

                return jret;

                //return ret; // masterListBO.GetNewLists(_lastUpdated);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="lastUpdated"></param>
        /// <returns></returns>
        public AdministrativeUnitTablet GetNewPagingTablet(string lastUpdated, string pageSize, string pageIndex)
        {
            //HeaderInfo h = new HeaderInfo();
            AdministrativeUnitTablet jret = new AdministrativeUnitTablet();
            try
            {
                if (string.IsNullOrEmpty(lastUpdated))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };


                    jret.ErrorCode = -1;
                    jret.ErrorMessage = "Thiếu tham số";
                    jret.CurrentTime = DHQGHN.Common.Utils.GetCurrentTimeNow();
                    jret.Data = null;

                    return jret;
                }

                string formatString = "yyyyMMddHHmmss";
                //string sample = "20100611221912";
                DateTime _lastUpdated;
                try
                {
                    _lastUpdated = DateTime.ParseExact(lastUpdated, formatString, null);
                }
                catch (Exception ex)
                {
                    logger.Error(ex.ToString());
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Tham số PageSize (hoặc PageIdx) sai định dạng!",
                        Description = "Tham số PageSize (hoặc PageIdx) sai định dạng!"
                    };

                    jret.ErrorCode = -2;
                    jret.ErrorMessage = "Tham số PageSize (hoặc PageIdx) sai định dạng!";
                    jret.CurrentTime = DHQGHN.Common.Utils.GetCurrentTimeNow();
                    jret.Data = null;

                    return jret;


                    //throw new FaultException<Fault.ValidationFault>(fault);

                }

                int _pageSize = 0, _pageIndex = 0;

                try
                {
                    _pageSize = System.Convert.ToInt32(pageSize);
                }
                catch
                {

                }

                try
                {
                    _pageIndex = System.Convert.ToInt32(pageIndex);
                }
                catch
                {

                }

                List<AdministrativeUnit> ret = adminUnitBO.GetNewPaging(_lastUpdated, _pageSize, _pageIndex);

                jret.ErrorCode = 200;
                jret.ErrorMessage = "OK";
                jret.CurrentTime = DHQGHN.Common.Utils.GetCurrentTimeNow();
                jret.Data = ret;

                return jret;

                //return ret; // masterListBO.GetNewLists(_lastUpdated);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public List<AdministrativeUnit> GetByIdParentTablet(string code, string levels)
        {
            try
            {
                //const int LEVELS_PROVINCE = 1;
                //AdministrativeUnitTablet jret = new AdministrativeUnitTablet();
                int iLevels = 0;
                int.TryParse(levels, out iLevels);

                List<AdministrativeUnit> ret = adminUnitBO.GetByIdParentTablet(code, iLevels);
                //jret.ErrorCode = 200;
                //jret.ErrorMessage = "OK";
                //jret.CurrentTime = DHQGHN.Common.Utils.GetCurrentTimeNow();
                //jret.Data = ret;
                return ret;
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public List<AdministrativeUnit> GetByPCODE(string pcode)
        {
            try
            {
                List<AdministrativeUnit> ret = adminUnitBO.GetByPCODE(pcode);
                return ret;
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public List<ThongKeDS> GetByCode(ModelAreaListThongKe adm)
        {
            try
            {
                return adminUnitBO.GetByCode(adm);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }        
    }
}